using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using Newtonsoft.Json;
using System.Management;

namespace NEM
{
    /// <summary>
    /// This Static class contains all the Extensions that are used in this project.
    /// </summary>
    static class  Extensions
    {
        /// <summary>
        /// Gets the property.
        /// </summary>
        /// <param name="pManObj">The p man object.</param>
        /// <param name="pName">Name of the p.</param>
        /// <returns>The Property Requested single String</returns>
        public static string GetProp(this ManagementObject pManObj, string pName)
        {
            string lvResult = string.Empty;

            try
            {
                lvResult = ((string[])pManObj[pName])[0];
            }
            catch (Exception)
            {
                lvResult = string.Empty;
            }


            return lvResult;
        }

        /// <summary>
        /// Gets the property list.
        /// </summary>
        /// <param name="pManObj">The p man object.</param>
        /// <param name="pName">Name of the p.</param>
        /// <returns>Returns the Requested properties.</returns>
        public static List<string> GetPropList(this ManagementObject pManObj, string pName)
        {
            List<string> lvResult = new List<string>();

            try
            {
                foreach (string lvItem in ((string[])pManObj[pName]))
                {
                    lvResult.Add(lvItem);  
                } 
            }
            catch (Exception)
            {
                lvResult = new List<string>();
            }


            return lvResult;
        }

        /// <summary>
        /// Serializes the specified object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns>Serialized string of the Object.</returns>
        /// <exception cref="System.NotSupportedException"></exception>
        public static string Serialize(this object obj)
        {
            if (obj.GetType().IsSerializable)
            {
                using (var stream = new MemoryStream())
                {
                    var serializer = new XmlSerializer(obj.GetType());
                    serializer.Serialize(stream, obj);
                    var bytes = new byte[stream.Length];
                    stream.Position = 0;
                    stream.Read(bytes, 0, bytes.Length);

                    return Encoding.UTF8.GetString(bytes);
                }
            }
            throw new NotSupportedException(string.Format("{0} is not serialize.", obj.GetType()));
        }

        /// <summary>
        /// De-serializes the specified serialized data.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serializedData">The serialized data.</param>
        /// <returns>De-Serialized object</returns>
        public static T Deserialize<T>(this string serializedData)
        {
            var serializer = new XmlSerializer(typeof(T));
            var reader = new XmlTextReader(new StringReader(serializedData));

            return (T)serializer.Deserialize(reader);
        }

        /// <summary>
        /// Serialize the passed object using Json.
        /// </summary>
        /// <param name="pObj">The p object.</param>
        /// <returns>The Serialized object in string format.</returns>
        public static string ToJson(this object pObj)
        {
            string lvResult = string.Empty;

            try
            {
                lvResult = JsonConvert.SerializeObject(pObj);
            }
            catch(Exception ex)
            {
                lvResult = string.Empty;
            }

            return lvResult;
        }

        /// <summary>
        /// De-serialize object form Json string 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pStr">The p string.</param>
        /// <returns>The De-serialized object.</returns>
        public static T FromJson<T>(this string pStr)
        {
            object lvResult = null;

            try
            {
                lvResult = JsonConvert.DeserializeObject<T>(pStr);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                lvResult = null;
            }

            return (T)lvResult;
        }

        /// <summary>
        /// Formats the string.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        public static string FormatString(this string sender, params object[] args)
        {
            return string.Format(sender, args);
        }

    }
}

